package com.samapp.review;

import java.util.ArrayList;
import java.util.List;

import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.entity.Indent;
import com.samapp.entity.IndentDetail;
import com.samapp.entity.ReviewVo;
import com.samapp.entity.Samples;
import com.samapp.indent.AutographActivity;
import com.samapp.indent.IndentDetailsActivity;
import com.samapp.utils.json.GsonUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.SimpleAdapter;

public class ReviewIndentActivity extends Activity implements OnClickListener  {
	private MyApplication myApplication = MyApplication.getApplicationInstance();
	
	/** 返回按钮 */
	private Button btnBack = null;
	/**订单信息预览*/
	private WebView webView = null;
	/** 上一步 */
	private Button btnFront = null;
	/** 下一步 */
	private Button btnNext = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review_indent);
        init();
    }
	
	public void init(){
		initView();
		initListener();
		initData();
	}
	
	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		webView = (WebView) findViewById(R.id.wv_review);
		btnFront = (Button) findViewById(R.id.btn_front);
		btnNext = (Button) findViewById(R.id.btn_next);
	}
	
	private void initListener() {
		btnBack.setOnClickListener(this);
		btnFront.setOnClickListener(this);
		btnNext.setOnClickListener(this);
	}
	
	public void initData(){
		WebSettings ws = webView.getSettings();
        // 启用JavaScript
        ws.setJavaScriptEnabled(true);
        // 加载预览网页
        Indent indent = myApplication.newIndent;
		if(indent != null){
			List<ReviewVo> reviews = new ArrayList<ReviewVo>();
			List<IndentDetail> details = indent.getOrderDetail();
			List<Samples> samples = indent.getSamples();
			for(Samples sample : samples){
				for(IndentDetail detail : details){
					if(sample.getSn().equals(detail.getSampleSn())){
						ReviewVo reviewVo = new ReviewVo();
						reviewVo.setCname(sample.getCname());
						reviewVo.setQty(sample.getQty());
						reviewVo.setModel(sample.getModel());
						reviewVo.setProduceUnitcn(sample.getProduceUnitcn());
						reviewVo.setProduceDate(sample.getProduceDate());
						reviewVo.setQualityDate(sample.getQualityDate());
						reviewVo.setBasis(sample.getBasis());
						reviewVo.setItemName(detail.getTestItemName());
						
						reviews.add(reviewVo);
					}
				}
			}
			String jsonData = GsonUtil.getGson().toJson(reviews);
			webView.loadUrl(myApplication.url2+"confirm.htm?json="+jsonData);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_back:
				skip(IndentDetailsActivity.class);
				finish();
				break;
			case R.id.btn_front:
				skip(IndentDetailsActivity.class);
				finish();
				break;
			case R.id.btn_next:
				skip(AutographActivity.class);
				finish();
				break;
		}
	}
	
	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}

	/** 修改返回键 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		btnFront.performClick();
	}
}
